<template>
  <div class="app-container">
    <div class="ProductionDynamic">
      <!-- 头部 -->
      <div class="ProductionDynamicHeader">
        <Find :OriginalFilter="{ static: true }" :formList="formList" @findClicked="findClicked" />
      </div>
      <el-card shadow="never" :body-style="{ padding: '5px 10px 5px' }" class="mt10">
        <div slot="header" class="f16">
          <span style="font-weight: 600">{{ $t(`Print.GenerateAProductionDynamicTable`) }}</span>
        </div>
        <div class="mt10">
          <span class="mr10">{{ $t(`Print.Date`) }}</span>
          <el-input style="width: 120px" v-model="currentDate" disabled placeholder="" class="MainRightMainPoInput mr24" />
          <span class="mr10">{{ $t(`Print.AccountingTime`) }}</span>
          <el-input style="width: 150px" disabled v-model="checkDate" placeholder="" class="MainRightMainPoInput mr24" />
          <el-button class="primaryBth" type="primary" size="small" @click="GenerateProduction">{{
            $t(`Print.GenerateAProductionDynamicTable`)
          }}</el-button>
        </div>
      </el-card>
      <el-card shadow="never" :body-style="{ padding: '0px 10px 5px' }">
        <div slot="header" class="f16">
          <span style="font-weight: 600">{{ $t(`Print.ProductionDynamicQuery`) }}</span>
        </div>
        <Table
          :tableHeader="tableHeader"
          :tableData="tableData"
          class="mt10"
          :TableHeight="100"
          :outTableHeight="150"
          :summaryData="summaryData"
          summary
          @currentRow="currentRow"
          :isLoading="isLoading"
        />
        <PageModule :PageData="{ total }" @pageSizeChange="handleSizeChange" @pageChange="handleCurrentChange" />
      </el-card>
    </div>

    <el-drawer v-model="drawer" :direction="'ttb'" size="small" :with-header="false">
      <span @click="drawer = false" style="float: right; cursor: pointer; color: rgb(7, 7, 7)">X</span>
      <div class="flex AIC" style="flex-direction: column">
        <div class="flex mt10 JCC" style="width: 100%; background-color: white">
          <el-card shadow="never" :body-style="{ padding: '5px 10px 5px' }">
            <h1 class="f16">{{ $t(`Print.DefinitionOfTheNumberOfDaysAheadOfMolding`) }}</h1>
            <Find :searchImg="false" :OriginalFilter="{ static: true }" :formList="formList10" @findClicked="findClicked10" />
          </el-card>
        </div>
      </div>
    </el-drawer>

    <el-dialog title="结束指令" v-model="dialogVisible" width="40%" class="dialog pb0">
      <div style="display: flex">
        <div>
          <Find :searchImg="false" :OriginalFilter="{ static: true }" :formList="formList9" />
          <Table @currentRow="currentRow9" @selectList="selectList" :tableHeader="tableHeader9" :tableData="tableData9" :TableHeight="45" />
        </div>
      </div>
      <PageModule :PageData="{ total: total9 }" @pageSizeChange="handleSizeChange9" @pageChange="handleCurrentChange9" />
      <el-button style="margin-top: 20px" type="primary" size="default" @click="cancelMarked">{{ $t(`Print.Unlabel`) }}</el-button>
    </el-dialog>
  </div>
</template>
<script lang="ts">
import { defineComponent, onMounted, ref, watch } from 'vue'
import SynchronizationLoginWork from '@/utils/SynchronizationLoginWork'
import fileRequest from '@/utils/fileRequest'
import { debounce } from 'lodash'
import JMessage from '@/utils/JMessage'
export default defineComponent({
  name: 'ProductionDynamic'
})
</script>
<script lang="ts" setup>
import { i18n, Jzhcn } from '@/i18n'
import Find from '@/components/Find/index.vue'
import { selectList, selectSomeList } from '@/plugins/mixin'
import service from '@/utils/request'
import { ElMessage } from 'element-plus'
import Api from '@/api/GeneralManager/inquire/AggregateSchedulingLi'
const dialogVisible = ref(false)
const drawer = ref(false)

const formList = ref([
  {
    type: 'input',
    inputPlaceholder: '请输入指令',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入型体',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入颜色',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入客户',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入客人货号',
    inputValue: ''
  },
  {
    type: 'date',
    dateLabel: '交期',
    dateValue: ''
  },

  {
    type: 'date',
    dateLabel: '下单日期',
    dateValue: ''
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '生产线',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '季度',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'checkbox',
    checkboxLabel: '未结束',
    checked: true
  },
  {
    type: 'checkbox',
    checkboxLabel: '已结束',
    checked: true
  },
  {
    type: 'button',
    checkboxLabel: '置空',
    bgColor: '#1890FF'
  },
  {
    type: 'button',
    checkboxLabel: '导出生产动态表'
  },
  // {
  //   type: 'button',
  //   checkboxLabel: '导出生产周期表'
  // },
  {
    type: 'button',
    checkboxLabel: '时程门槛定义'
  },
  {
    type: 'button',
    checkboxLabel: '查询标注结束指令'
  },
  {
    type: 'button',
    checkboxLabel: '标注指令已结束'
  }
])
const formList10 = ref([
  {
    type: 'input',
    inputLabel: '冲裁',
    inputValue: ''
  },
  {
    type: 'input',
    inputLabel: '高频',
    inputValue: ''
  },
  {
    type: 'input',
    inputLabel: '配套中心',
    inputValue: ''
  },
  {
    type: 'input',
    inputLabel: '针车',
    inputValue: ''
  },
  {
    type: 'input',
    inputLabel: '大底',
    inputValue: ''
  },
  {
    type: 'button',
    checkboxLabel: '保存'
  },
  {
    type: 'button',
    checkboxLabel: '返回'
  }
])
const tableData = ref([])
const tableHeader = ref([
  {
    label: '客户',
    prop: 'custom',
    width: '76px'
  },
  {
    label: '指令',
    prop: 'lotNo',
    width: '76px'
  },
  {
    label: '型体',
    prop: 'style',
    width: '76px'
  },
  {
    label: '配色',
    prop: 'color',
    width: '100px'
  },
  {
    label: '订单量',
    prop: 'orderQty',
    width: '58px'
  },
  {
    label: '预补单',
    prop: 'surplusQty',
    width: '58px'
  },
  {
    label: '下单日期',
    prop: 'orderDate',
    width: '58px'
  },
  {
    label: '客户交期',
    prop: 'deliveryDate',
    width: '58px'
  },
  {
    width: '110',
    label: '客人货号',
    prop: 'custStyle'
  },
  {
    width: '110',
    label: '季度',
    prop: 'season'
  },
  {
    Multistage: true,
    label: '斩刀试作',
    children: [
      {
        label: '计划日期',
        prop: 'purPlanDate'
      },
      {
        label: '实际日期',
        prop: 'purActualDate'
      }
    ]
  },
  {
    label: '欠料',
    prop: 'schQty',
    width: '58px'
  },
  {
    Multistage: true,
    label: '主面料',
    children: [
      {
        label: '计划日期',
        prop: 'whPlanDate'
      },
      {
        label: '实际日期',
        prop: 'whActualDate'
      }
    ]
  },
  {
    Multistage: true,
    label: '冲裁',
    children: [
      {
        label: '冲裁组别',
        prop: 'cutDept',
        width: '100px'
      },
      {
        label: '裁断当日',
        prop: 'cutProd',
        width: '58px'
      },
      {
        label: '裁断欠数',
        prop: 'cutShort',
        width: '58px'
      },
      {
        label: '计划完成',
        prop: 'cutPlanDate',
        width: '58px'
      },
      {
        label: '实际完成',
        prop: 'cutActualDate',
        width: '58px'
      }
    ]
  },
  {
    Multistage: true,
    label: '工艺',
    children: [
      {
        label: '工艺欠数',
        prop: 'hfShort',
        width: '58px'
      },
      {
        label: '计划完成',
        prop: 'hfPlanDate',
        width: '58px'
      },
      {
        label: '实际完成',
        prop: 'hfActualDate',
        width: '58px'
      }
    ]
  },
  {
    Multistage: true,
    label: '配套',
    children: [
      {
        label: '配套当日',
        prop: 'partProd',
        width: '58px'
      },
      {
        label: '配套欠数',
        prop: 'partShort',
        width: '58px'
      },
      {
        label: '计划完成',
        prop: 'partPlanDate',
        width: '58px'
      },
      {
        label: '实际完成',
        prop: 'partActualDate',
        width: '58px'
      }
    ]
  },
  {
    Multistage: true,
    label: '针车',
    children: [
      {
        label: '针车组别',
        prop: 'stitchDept',
        width: '58px'
      },
      {
        label: '当日产量',
        prop: 'stitchProd',
        width: '58px'
      },
      {
        label: '针车欠数',
        prop: 'stitchShort',
        width: '58px'
      },
      {
        label: '计划完成',
        prop: 'stitchPlanDate',
        width: '58px'
      },
      {
        label: '实际完成',
        prop: 'stitchActualDate',
        width: '58px'
      }
    ]
  },
  {
    Multistage: true,
    label: '鞋面仓',
    children: [
      {
        label: '库存',
        prop: 'upperStock'
      }
    ]
  },
  {
    Multistage: true,
    label: '大底',
    children: [
      {
        label: '大底厂商',
        prop: 'soleDept'
      },
      {
        label: '当日入库',
        prop: 'soleProd'
      },
      {
        label: '欠数',
        prop: 'soleShort'
      },
      {
        label: '计划完成',
        prop: 'solePlanDate'
      },
      {
        label: '实际完成',
        prop: 'soleActualDate'
      }
    ]
  },
  {
    Multistage: true,
    label: '鞋垫',
    children: [
      {
        label: '欠数',
        prop: 'soleShort2'
      }
    ]
  },
  {
    Multistage: true,
    label: '成型投料',
    children: [
      {
        label: '成型组别',
        prop: 'lastDept'
      },
      {
        label: '当日投料',
        prop: 'lastProd'
      },
      {
        label: '投料欠数',
        prop: 'lastShort'
      },
      {
        label: '计划完成',
        prop: 'lastPlanDate'
      },
      {
        label: '实际完成',
        prop: 'lastActualDate'
      }
    ]
  },
  {
    Multistage: true,
    label: '成型产出',
    children: [
      {
        label: '当日产出',
        prop: 'finishProd'
      },
      {
        label: '欠数',
        prop: 'finishShort'
      },
      {
        label: '计划完成',
        prop: 'finishPlanDate'
      },
      {
        label: '实际完成',
        prop: 'finishActualDate'
      }
    ]
  }
])
// 表一模块
// '置空'按钮
const findClicked = async (content: string) => {
  if (content === '置空') {
    formList.value.forEach((item) => {
      item.inputValue = ''
      item.selectValue = ''
      item.dateValue = ''
    })
    getTableList()
  }
  if (content === '导出生产动态表') {
    const res = (await fileRequest.put(`/PcProdStatus2/export`, {
      color: formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入颜色'
      )?.inputValue, //	颜色
      custom: formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入客户'
      )?.inputValue, //	客户
      deliveryEndDate: formList.value.find(
        (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '交期'
      )?.dateValue?.[1], //	交期日期结束
      deliveryStartDate: formList.value.find(
        (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '交期'
      )?.dateValue?.[0], //	交期日期开始
      flag: cfmFlag(), //	Y 已结束 N 未结束
      goods: formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入客人货号'
      )?.inputValue, //	货号
      line: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '生产线')
        ?.selectValue, //	生产线
      lotNo: formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入指令'
      )?.inputValue, //	指令
      orderEndDate: formList.value.find(
        (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '下单日期'
      )?.dateValue?.[1], //	下单日期结束
      orderStartDate: formList.value.find(
        (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '下单日期'
      )?.dateValue?.[0], //	下单日期开始
      pageNum: currentPage.value, //
      pageSize: pageSize.value, //
      season: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '季度')
        ?.selectValue, //	季度
      style: formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入型体'
      )?.inputValue //	型体
    })) as any
    const link = document.createElement('a') // 利用a标签自定义下载文件名
    let blob = new Blob([res.data]) // 创建Blob对象，设置文件类型
    link.style.display = 'none'
    //设置连接
    link.href = window.URL.createObjectURL(blob)
    const FileName = decodeURI(res.headers['content-disposition'].split(`utf-8''`)[1])
    link.download = FileName //自定义文件名字
    document.body.appendChild(link)
    //模拟点击事件
    link.click()
    document.body.removeChild(link)
  }
  if (content === '导出生产周期表') {
    JMessage.error('没写接口')
  }
  if (content === '时程门槛定义') {
    drawer.value = true
    getDrawList()
  }
  if (content === '查询标注结束指令') {
    dialogVisible.value = true
    getEndLotNo()
  }
  if (content === '标注指令已结束') {
    const res = (await SynchronizationLoginWork.post(`/PcProdStatus2/labelFlag?lotNo=${currentTableRow.value.lotNo}`)) as any
    if (res.code === 200) {
      JMessage.success('标注指令已结束成功')
      getTableList()
    } else {
      JMessage.error(res.message)
    }
  }
}
//查询标注结束指令函数
const currentTableRow9 = ref({})
const currentRow9 = async (row: any) => {
  currentTableRow9.value = row
}

const formList9 = ref([
  {
    type: 'input',
    inputLabel: '指令',
    inputValue: ''
  }
])
watch(formList9.value, () => {
  getEndLotNo()
})
const tableData9 = ref([])
const tableHeader9 = ref([
  {
    label: '指令',
    prop: 'lotNo'
  }
])
// 取消标注
const cancelMarked = async () => {
  const res = (await SynchronizationLoginWork.post('/PcProdStatus2/cancelLabelFlag', currentTableRow9.value)) as any
  if (res.code === 200) {
    JMessage.success('取消标注成功')
    getEndLotNo()
  } else {
    JMessage.error(res.message)
  }
}
const currentPage9 = ref(1)
const pageSize9 = ref(40)
const total9 = ref(0)
const handleSizeChange9 = (val: number) => {
  pageSize9.value = val
  getEndLotNo()
}
const handleCurrentChange9 = (val: number) => {
  currentPage9.value = val
  getEndLotNo()
}
const getEndLotNo = async () => {
  const res = (await SynchronizationLoginWork.post('/PcProdStatus2/getLotNos', {
    companyId: '',
    lotNo: formList9.value[0].inputValue,
    pageNum: currentPage9.value,
    pageSize: pageSize9.value,
    style: ''
  })) as any
  if (res.code === 200) {
    tableData9.value = res.data.list
    total9.value = res.data.total
  } else {
    JMessage.error(res.message)
  }
}
const findClicked10 = async (content: string) => {
  if (content === '返回') {
    drawer.value = false
  }
  if (content === '保存') {
    const res = (await SynchronizationLoginWork.put('/PcProdStatus2/update', {
      ...currentDraw.value,
      cutting01: formList10.value[0].inputValue,
      cutting02: formList10.value[1].inputValue,
      cutting03: formList10.value[2].inputValue,
      stitching: formList10.value[3].inputValue,
      sole: formList10.value[4].inputValue
    })) as any
    if (res.code === 200) {
      JMessage.success('保存成功')
      drawer.value = false
      getTableList()
    } else {
      JMessage.error(res.message)
    }
  }
}
const currentDraw = ref({})
const getDrawList = async () => {
  const res = (await SynchronizationLoginWork.get('/PcProdStatus2/getTime')) as any
  if (res.code === 200) {
    currentDraw.value = res.data
    formList10.value[0].inputValue = res.data.cutting01
    formList10.value[1].inputValue = res.data.cutting02
    formList10.value[2].inputValue = res.data.cutting03
    formList10.value[3].inputValue = res.data.stitching
    formList10.value[4].inputValue = res.data.sole
  } else {
    JMessage.error(res.message)
  }
}
const imgList = ref([])
// 总计
interface summaryDataType {
  index: number
  num: number | undefined
}
const summaryData = ref<summaryDataType[]>([
  {
    index: 4,
    num: 0
  },
  {
    index: 5,
    num: 0
  },
  {
    index: 14,
    num: 0
  },
  {
    index: 21,
    num: 0
  },
  {
    index: 22,
    num: 0
  },
  {
    index: 30,
    num: 0
  },
  {
    index: 38,
    num: 0
  },
  {
    index: 42,
    num: 0
  }
])
// 分页器
const currentPage = ref(1)
const pageSize = ref(40)
const total = ref(0)
const handleSizeChange = (val: number) => {
  pageSize.value = val
  getTableList()
}
const handleCurrentChange = (val: number) => {
  currentPage.value = val
  getTableList()
}
// 表格计算函数
const cfmFlag = () => {
  const Unaudited = formList.value.find((item: any) => item.type === 'checkbox' && i18n.global.t(item.checkboxLabel) === '未结束')
  const Audited = formList.value.find((item: any) => item.type === 'checkbox' && i18n.global.t(item.checkboxLabel) === '已结束')
  if (!Unaudited || !Audited) return
  if (Unaudited.checked && Audited.checked) {
    return ''
  } else if (Unaudited.checked) {
    return 'N'
  } else if (Audited.checked) {
    return 'Y'
  } else {
    ElMessage('必须至少选择一项')
    Unaudited.checked = true
    Audited.checked = true
    return ''
  }
}
const isLoading = ref(true)

// 获取表数据
const getTableList = debounce(async () => {
  isLoading.value = true
  let tableDataList = await service.post<any, any>('/PcProdStatus2/getProductionDynamicQuery', {
    color: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入颜色'
    )?.inputValue, //	颜色
    custom: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入客户'
    )?.inputValue, //	客户
    deliveryEndDate: formList.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '交期'
    )?.dateValue?.[1], //	交期日期结束
    deliveryStartDate: formList.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '交期'
    )?.dateValue?.[0], //	交期日期开始
    flag: cfmFlag(), //	Y 已结束 N 未结束
    goods: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入客人货号'
    )?.inputValue, //	货号
    line: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '生产线')
      ?.selectValue, //	生产线
    lotNo: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入指令'
    )?.inputValue, //	指令
    orderEndDate: formList.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '下单日期'
    )?.dateValue?.[1], //	下单日期结束
    orderStartDate: formList.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '下单日期'
    )?.dateValue?.[0], //	下单日期开始
    pageNum: currentPage.value, //
    pageSize: pageSize.value, //
    season: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '季度')
      ?.selectValue, //	季度
    style: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入型体'
    )?.inputValue //	型体
  })
  isLoading.value = false
  tableData.value = tableDataList.queryData
  total.value = tableDataList.total
  summaryData.value[0].num = tableDataList.count.orderCount
  summaryData.value[1].num = tableDataList.count.surplusCount
  summaryData.value[2].num = tableDataList.count.cutProdCount
  summaryData.value[3].num = tableDataList.count.partProdCount
  summaryData.value[4].num = tableDataList.count.partShortCount
  summaryData.value[5].num = tableDataList.count.upperStockCount
  summaryData.value[6].num = tableDataList.count.lastProdCount
  summaryData.value[7].num = tableDataList.count.finishProdCount
  getCheckDate()
  currentTime()
}, 500)
onMounted(() => {
  getTableList()
  getSelectList()
})
watch(
  () =>
    formList.value.map((item: any) => {
      item.selectValue, item.inputValue, item.dateValue, item.checked
    }),
  () => {
    getTableList()
  }
)
// 获取下拉框数据
const getSelectList = async () => {
  await selectList(formList, () => service.get('/back/order/allOrderCondition'))
  await selectSomeList(formList, Api.getSelectList2)
}

// 点击表给另一张表传数据
const currentTableRow = ref({}) as any
const currentRow = async (row: any = tableData.value[0] ? tableData.value[0] : {}) => {
  currentTableRow.value = row
}

const GenerateProduction = async () => {
  const res = (await SynchronizationLoginWork.post('/PcProdStatus2/GenerateProductionDynamics')) as any
  if (res.code === 200) {
    JMessage.success(res.message)
    getCheckDate()
    getTableList()
  } else {
    JMessage.error(res.message)
  }
}
const checkDate = ref('')
const getCheckDate = async () => {
  const res = (await SynchronizationLoginWork.get('/PcProdStatus2/ReturnTime')) as any
  checkDate.value = res.data
}
const currentDate = ref('')
const currentTime = () => {
  const now = new Date()
  const year = now.getFullYear()
  const month = String(now.getMonth() + 1).padStart(2, '0') // 月份从0开始，所以加1
  const day = String(now.getDate()).padStart(2, '0')
  currentDate.value = `${year}-${month}-${day}` // 返回格式为 YYYY-MM-DD
}
</script>

<style lang="less" scoped>
// Input 输入框的样式
.MainRightMainPoInput {
  width: 88px;
  height: 25px;
}
</style>
